package com.example.lambda;

import org.junit.Test;

import java.util.Comparator;

/**
 * @author readpage
 * @create 2022-10-22 10:23
 * Lambda表达式的使用举例
 */
public class LambdaTest {
    @Test
    public void test() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("hello world!");
            }
        };
        runnable.run();

        System.out.println("***************");
        Runnable r2 = () -> System.out.println("hello world!");
        r2.run();
    }

    @Test
    public void test2() {
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };

        System.out.println(comparator.compare(12, 21));

        System.out.println("******************************");

        //Lambda表达式的写法
        Comparator<Integer> com2 = (o1, o2) -> Integer.compare(o1, o2);
        System.out.println(com2.compare(32, 21));

        System.out.println("********************");

        //方法引用
        Comparator<Integer> com3 = Integer::compare;
        System.out.println(com2.compare(32, 21));
    }
}
